﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;

namespace MainService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CommonService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CommonService.svc or CommonService.svc.cs at the Solution Explorer and start debugging.
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CommonService : ICommonService
    {

        public int AddDatabase(string dbName, bool isDataCaseSenitive, int loggedInUserId, byte[] data, List<int> associatedServices)
        {
            int newDBId = 0;
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {

                dbDetails.DBName = dbName;
                dbDetails.IsCaseSensitiveValidation = isDataCaseSenitive;
                dbDetails.CreatedBy = loggedInUserId;
                dbDetails.ServiceDetails = associatedServices;
                if (data != null && data.Length > 0)
                {
                    dbDetails.ScanDataList = dbDetails.GetScanDataList(data);
                }
                msgs = dbDetails.CreateDatabase(ref newDBId);
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return newDBId;
        }

        public string DeleteDatabase(int dbId, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {
                _dbDetails.DbId = dbId;
                _dbDetails.CreatedBy = loggedInUserId;
                msgs = _dbDetails.DeteleDB();
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        public string RenameDatabase(int dbId, string newDBName, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                dbDetails.DbId = dbId;
                dbDetails.DBName = newDBName;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.RenameDB();
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        public string AssociateDBToService(int dbId, List<int> serviceId, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
               
                dbDetails.DbId = dbId;
                dbDetails.ServiceDetails = serviceId;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.AssociateServiceToDb();
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        public DatabaseDetails GetDbDetail(int DbId, int loggedInUserId)
        {
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {               
                _dbDetails.DbId = DbId;
                _dbDetails.CreatedBy = loggedInUserId;
                _dbDetails = _dbDetails.GetDatabaseDetails();
            }
            catch (Exception ex)
            {
                _dbDetails.UserMessage = ex.Message;
            }
            return _dbDetails;
        }

        public List<DatabaseDetails> GetClientDBList(int clientId)
        {
            List<DatabaseDetails> lstDB = new List<DatabaseDetails>();
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {
                List<int> dbIds = new List<int>();
                dbIds = _dbDetails.GetDBIDList(clientId);
                for (int i = 0; i < dbIds.Count; i++)
                {
                    lstDB.Add(GetDbDetail(dbIds[i], clientId));
                }            

            }
            catch (Exception ex)
            {
                
            }
            return lstDB;
        }

        public string ImportData(byte[] data, int dbId, int loggedInUserId)
        {
            string msgs = "";
            try
            {
                List<ScanData> ScanDataList = null;
                DatabaseDetails dbDetails = new DatabaseDetails();
                if (data != null && data.Length > 0)
                {
                    ScanDataList = new List<ScanData>();
                    ScanDataList = dbDetails.GetScanDataList(data);
                }
                if (ScanDataList.Count>0)
                {
                    foreach (ScanData item in ScanDataList)
                    {
                       msgs = AddScanData(dbId, loggedInUserId, item.BarcodeVale, item.BarcodeResponse, item.Validity);
                       msgs = " " + msgs + " ";
                    }
                }
              
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }

            return msgs;
        }

        public byte[] ExportData(int dbId, int loggedInUserId)
        {
            DatabaseDetails _dbDetail = new DatabaseDetails();
            _dbDetail.DbId = dbId;
            _dbDetail.CreatedBy = loggedInUserId;
            string strScanData = _dbDetail.GetTableData();
           return Encoding.GetEncoding("iso-8859-1").GetBytes(strScanData);
        }
                
        public string AddScanData(int dbId, int loggedInUserId, string barcodValue, string barcodeResponseValue, int validity)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                List<ScanData> lstData = new List<ScanData>();
                ScanData _data = new ScanData();
                _data.BarcodeVale = barcodValue;
                _data.Validity = validity;
                _data.BarcodeResponse = barcodeResponseValue;
                lstData.Add(_data);
                dbDetails.ScanDataList = lstData;
                dbDetails.DbId = dbId;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.AddScanData();
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        public Dictionary<int, string> GetServices(int clientId, string hashCode)
        {
            ServiceDetails _service = new ServiceDetails();
            return _service.GetServiceList(clientId, hashCode);
        }
    }
}
